const cart = new Vue({
    el: '#cart',
    data: {
        books: [{
            name: '《算法导论》',
            date: '2006-9',
            price: 85.00,
            count: 1
        }, {
            name: '《UNIX编程艺术》',
            date: '2006-2',
            price: 59.00,
            count: 1
        }, {
            name: '《编程珠玑》',
            date: '2008-10',
            price: 39.00,
            count: 1
        }, {
            name: '《代码大全》',
            date: '2006-3',
            price: 128.00,
            count: 1
        }]
    },
    methods: {
        increment(index) {
            const book = this.books[index];
            book.count++;
        },
        decrement(index) {
            const book = this.books[index];
            if (book.count > 1) {
                book.count--;
            }
        },
        remove(index) {
            this.books.splice(index, 1);
        }
    },
    computed: {
        totalPrice() {
            let result = 0.0;
            // 1. of
            // for (let book of this.books) {
            //     result += book.price * book.count;
            // }
            // 2. in
            // for (let i in this.books) {
            //     const book = this.books[i];
            //     result += book.price * book.count;
            // }
            // 3. reduce
            result = this.books.reduce((num, book) => num + book.price * book.count, 0);
            return result;
        }
    },
    // 过滤器的使用
    filters: {
        showPrice(price) {
            return '¥' + price.toFixed(2);
        }
    }
});
